<?php

namespace App\Http\Controllers\Backend\Message;

use App\Http\Requests\Backend\Message\HeaderMessageIndexRequest;
use App\Http\Requests\Backend\Message\HeaderMessageStoreRequest;
use App\Http\Requests\Backend\Message\HeaderMessageUpdateRequest;
use App\Models\HeaderMessage;
use App\Http\Controllers\Controller;
use App\Models\HeaderMessageReadeds;

class HeaderMessageController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:backend');
    }

    /**
     * Description:后台页面消息列表
     * Author: Sam
     * DateTime: 2019/1/12 9:56
     * @param HeaderMessageIndexRequest $request
     * @return mixed
     */
    public function index(HeaderMessageIndexRequest $request)
    {
        $page_size = $request->get('page_size',null);
        if($page_size){
            $list = HeaderMessage::orderByDesc('created_at')->paginate($page_size);
        }else{
            $list = HeaderMessage::orderByDesc('created_at')->get();
        }
        return success($list);
    }


    /**
     * Description:添加页面消息
     * Author: Sam
     * DateTime: 2019/1/12 9:52
     * @param HeaderMessageStoreRequest $request
     * @return mixed
     */
    public function store(HeaderMessageStoreRequest $request)
    {
        $params = $this->extractInputFromRules($request,$request->rules());
        $params['url'] = $params['url']??' ';
        HeaderMessage::create($params);
        return success([]);
    }

    /**
     * Description:单个消息模板
     * Author: Sam
     * DateTime: 2019/1/12 9:57
     * @param HeaderMessage $headerMessage
     * @return mixed
     */
    public function show(HeaderMessage $headerMessage)
    {
        return success($headerMessage);
    }

    /**
     * Description:修改消息模板
     * Author: Sam
     * DateTime: 2019/1/12 10:07
     * @param HeaderMessageUpdateRequest $request
     * @param HeaderMessage $headerMessage
     * @return mixed
     */
    public function update(HeaderMessageUpdateRequest $request, HeaderMessage $headerMessage)
    {
        $params = $this->extractInputFromRules($request,$request->rules());
        $headerMessage->updateLogic($params);
        return success([]);
    }

    /**
     * Description:删除消息模板
     * Author: Sam
     * DateTime: 2019/1/12 10:08
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        HeaderMessageReadeds::where('msg_id',$id)->delete();
        HeaderMessage::destroy($id);
        return success([]);
    }

    /**
     * Description:应用页面消息
     * Author: Sam
     * DateTime: 2019/1/12 10:22
     * @param HeaderMessage $headerMessage
     * @return mixed
     */
    public function enable(HeaderMessage $headerMessage)
    {
        $all = new HeaderMessage();
        $all->where('id','>',0)->update(['status'=>2]);
        $headerMessage->updateLogic(['status'=>1]);
        return success([]);
    }
}
